//
//  AppDelegate.swift
//  mySinaBlog
//
//  Created by whtapple on 3/21/16.
//  Copyright © 2016 whtapple. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        //创建 window
        window = UIWindow(frame: UIScreen.mainScreen().bounds);
        //设置颜色
        window?.backgroundColor = UIColor.whiteColor()
        //创建根控制器
        let rootController = accessController()
        //设置根控制器
        window?.rootViewController = rootController
        // 设置keywindow
        window?.makeKeyAndVisible()
        return true
    }
    //外界切换控制器调用的方法
    class func delegateChangeController(controller: UIViewController){
        let delegate = UIApplication.sharedApplication().delegate as? AppDelegate
        delegate?.window?.rootViewController = controller
    }
    //根据登陆和是否新版本情况，判断控制器入口
    func accessController() -> UIViewController {
       let islogin = HTUserAccountViewModel.sharedInstance.isLogin()
        if !islogin {
            return HTmainViewController()
        }else if newVersion() {
            return HTNewFeatureViewController()
        }else{
            return HTWelcomeViewController()
        }
    }
    //判断是否为新版本
    private func newVersion() -> Bool{
        //获得当前版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
//        print("currentVersion = \(currentVersion)")
        //获得上一版本
        let lastVerdionKey = "lastVerdionKey"
        let lastVersion = NSUserDefaults.standardUserDefaults().stringForKey(lastVerdionKey)
//         print("lastVersion = \(lastVersion)")
        //进行比较
        let isNew: Bool = currentVersion != lastVersion
        //存入沙盒
        if isNew{
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: "lastVerdionKey")
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        //返回
        return isNew
    }
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

